package com.common.manager;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.common.component.PageData;
import com.common.model.OrgInfoModel;
import com.common.model.StaffInfoModel;
import com.common.utils.MD5Crypt;
import com.framework.model.IData;
import com.framework.model.IDataset;
import com.framework.model.impl.DataMap;
import com.framework.model.impl.DatasetList;

import flex.messaging.io.amf.ASObject;


@Component
public class StaffManager  extends BaseManager{
	
	
	@Autowired
	private RoleInfoManager roleInfoManager;
	
	@Autowired
	private InterfaceManager interfaceManager;
	
	@Override
	public void setSqlMapNameSpace() {
		this.sqlMapNameSpace = "com.common.model.staffInfoModel";
	}
	
	
	public boolean checkStaffNum(String staffNum) throws Exception {
			
		IData conditionObj = new DataMap();
		conditionObj.put("staffNum", staffNum);
		StaffInfoModel staffInfoModel =(StaffInfoModel)super.getObjectInfo("checkExistsObject",conditionObj);
		if(staffInfoModel!=null){
			return false;
		}
		return true;
	}
		
	
	/**
	 * 功能：检查用户登录和密码校验
	 * @param staffNum
	 * @param password
	 * @return
	 * @throws Exception
	 */
	public StaffInfoModel checkLoginUser(String staffNum,String password) throws Exception {
		
		if(StringUtils.isNotBlank(staffNum)&& StringUtils.isNotBlank(password)){
			IData conditionObj = new DataMap();
			conditionObj.put("staffNum", staffNum);
			String password1=password;
			if(password!=null && !"".equals(password)){
				password1 = MD5Crypt.encrypt(password);
			}
			conditionObj.put("loginPwd", password1);
			return (StaffInfoModel)super.getObjectInfo("checkLoginUser",conditionObj);
		}else{
			return null;
		}
	}
	
	/**
	 * 添加用户信息
	 * @param staffInfoModel
	 * @return
	 */
	public int insertStaff(StaffInfoModel staffInfoModel){
		String password = staffInfoModel.getLoginPwd();
		if(password!=null && !"".equals(password)){
			password = MD5Crypt.encrypt(password);
			staffInfoModel.setLoginPwd(password);
		}
		return super.insertObjectAndGetID(staffInfoModel);
	}
	
	
	
	
	/**
	 * 添加用户信息
	 * @param staffInfoModel
	 * @return
	 */
	public int updateStaff(String newPwd,StaffInfoModel staffInfoModel){
		String password = staffInfoModel.getLoginPwd();
		if(!newPwd.equals(password)){
			if(password!=null && !"".equals(password)){
				password = MD5Crypt.encrypt(password);
				staffInfoModel.setLoginPwd(password);
			}else{
				staffInfoModel.setLoginPwd(null);
			}
		}
		return super.updateObject(staffInfoModel);
	}
}
